{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8ed3dd30-d3da-4290-88cb-48a74872a60f",
   "metadata": {},
   "source": [
    "# 一、统计分析函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08dbc619-3dd9-4a32-8e21-02ecc7a56dd5",
   "metadata": {},
   "source": [
    "## （1）数学运算函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "069b09e2-cad2-4f63-b0c3-5230e68219d1",
   "metadata": {},
   "source": [
    "### ① add"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d62efb34-be95-419b-978d-e3ddf2636a52",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "43c7c78c-c661-4520-9919-ba1a1bebede7",
   "metadata": {},
   "outputs": [],
   "source": [
    "n1 = np.array([1, 1])\n",
    "n2 = np.array([[1, 2], [1, 2]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2b66d575-f361-4279-b671-d797829ecc5a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [1, 2]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(n1, n2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6bb444e9-75c2-43e9-a094-5bb9e077a7e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 3],\n",
       "       [2, 3]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1 + n2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "441c6544-90cd-420a-b4a4-e45209805fcd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 3],\n",
       "       [2, 3]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.add(n1, n2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "165055a5-8b41-4af0-aa36-81670cf767dc",
   "metadata": {},
   "source": [
    "### ② subtract"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4e816606-60b9-423b-8770-ffab2186b5b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1],\n",
       "       [0, 1]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n2 - n1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "362ef9d1-6264-4d80-a075-6846f4c7d629",
   "metadata": {},
   "source": [
    "### ③ multiply"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1d4fb938-3605-4526-ae3c-82569faa995f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [1, 2]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1 * n2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6998bbb9-e472-402b-932a-d3b9e4d7ca1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [1, 2]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.multiply(n1, n2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "229cd3b8-fdcd-4e37-b242-b9f49803cd24",
   "metadata": {},
   "source": [
    "### ④ divide"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "0d8d483d-60e0-491b-86fe-82cfdad84287",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [1, 2]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n2 * n1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "26f4b859-d0ed-4801-8ae6-ab648044f6ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 2.],\n",
       "       [1., 2.]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.divide(n2, n1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "28c75664-1bec-492a-98fd-ebcb56f66ff0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1. , 0.5],\n",
       "       [1. , 0.5]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.divide(n1, n2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "388d510a-fb08-4111-bba1-82554dc97821",
   "metadata": {},
   "source": [
    "## ⑤ reciprocal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "5efe508f-3a0f-4900-a9a5-ab044960b97a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [1, 2]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "11b29d70-9baa-473b-8e15-2bf098f72d0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0],\n",
       "       [1, 0]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.reciprocal(n2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "97048d69-31b9-4fe1-9d3a-079688b7cf3d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int64')"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n2.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "6f4d8391-c763-4146-ae1f-5511819ae587",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 2.],\n",
       "       [3., 4.]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 浮点数的倒数才能正确得到\n",
    "n3 = np.arange(1, 5, dtype='float').reshape(2, 2)\n",
    "n3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "25b1a3de-d41b-45b2-bd3a-3abdf04188d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.        , 0.5       ],\n",
       "       [0.33333333, 0.25      ]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.reciprocal(n3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0be3a0c2-4e2f-4252-bda7-bac11d2d99ae",
   "metadata": {},
   "source": [
    "### ⑥ power"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "171dfaf7-cd25-4bd1-8151-ca01217a101e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [1, 2]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "95f18e7b-4653-42fd-9159-a7a3f6e25f7b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 4],\n",
       "       [1, 4]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.power(n2, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "5ebdb2bc-a8d2-40b0-a69d-2d9aad11705e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 8],\n",
       "       [1, 8]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.power(n2, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c40d56d-19df-4f3b-9ccd-0ca0fc042688",
   "metadata": {},
   "source": [
    "### ⑦ around"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "50c1d9b5-94a9-4db8-8af9-550919c4c5b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.02713644, 0.98919495],\n",
       "       [0.17436672, 0.01821132]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n4 = np.random.rand(2, 2)\n",
    "n4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "de8aa55b-0871-49e8-9606-4c7b5e286848",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.027, 0.989],\n",
       "       [0.174, 0.018]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.around(n4, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a096f962-38c4-42fe-b5b9-7460eca1cb90",
   "metadata": {},
   "source": [
    "### ⑧ ceil"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "cc83fb29-62b6-4667-b2ac-34ed28f2f1ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1.],\n",
       "       [1., 1.]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ceil(n4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5515f2eb-c4b0-4252-90d8-95ffdfc36fdd",
   "metadata": {},
   "source": [
    "### ⑨ floor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0ed7e83f-069b-4dfc-9ebb-edc9b25b09ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 0.],\n",
       "       [0., 0.]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.floor(n4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e5a0f99-461f-4c28-8341-39b7c8b2e0a9",
   "metadata": {},
   "source": [
    "### ⑩ abs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f00a4954-c3bf-4861-86e3-dfe7698bc967",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  6,   8,   6],\n",
       "       [ -3, -10,   4],\n",
       "       [ -5,   5,  -5]], dtype=int32)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n5 = np.random.randint(-10, 11, size=(3, 3))\n",
    "n5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "dbd4d719-59ee-48ab-8f1f-a7ec4c0fc6a7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 6,  8,  6],\n",
       "       [ 3, 10,  4],\n",
       "       [ 5,  5,  5]], dtype=int32)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.abs(n5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e07e53e-02b3-410d-a6e6-c66b7303f406",
   "metadata": {},
   "source": [
    "### ⑪ exp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3598bb0b-9c69-4383-be04-c06d0fb8a094",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4.03428793e+02, 2.98095799e+03, 4.03428793e+02],\n",
       "       [4.97870684e-02, 4.53999298e-05, 5.45981500e+01],\n",
       "       [6.73794700e-03, 1.48413159e+02, 6.73794700e-03]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.exp(n5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "844115f3-5fc0-4812-8580-e8c9361e4173",
   "metadata": {},
   "source": [
    "### ⑫ sqrt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "cd1726ce-cce8-43f4-9378-aa672ef18d45",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2.44948974, 2.82842712, 2.44948974],\n",
       "       [1.73205081, 3.16227766, 2.        ],\n",
       "       [2.23606798, 2.23606798, 2.23606798]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(np.abs(n5))  # 开方"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cac3f083-61b3-401d-a116-cac49c38f566",
   "metadata": {},
   "source": [
    "### ⑬ prod"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e8b1ab53-606a-4d83-a712-2d444ef3725d",
   "metadata": {},
   "outputs": [],
   "source": [
    "n6 = np.arange(1, 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d5fbfc2d-0279-4677-9f6a-f64cde694f5c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "32eec382-8566-4ef6-8ea1-08866efe73df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(362880)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.prod(n6)   # 连乘积"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2cc38a9d-6abb-4c77-8650-9a7be8bfea03",
   "metadata": {},
   "source": [
    "### 二、聚合函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce53e6f1-556b-445d-80a9-df72d6f9932d",
   "metadata": {},
   "source": [
    "### ① sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "dcb05553-60b1-4798-9918-53eadab159c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  6,   8,   6],\n",
       "       [ -3, -10,   4],\n",
       "       [ -5,   5,  -5]], dtype=int32)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "8e1bc6ca-3c5d-4166-8f48-11b292e23b1b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(6)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(n5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "2ef63951-93f6-4627-9567-65350d888bd1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-2,  3,  5])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(n5, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "748582bc-7c3c-4b0c-9167-b9007e24cab2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([20, -9, -5])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(n5, axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc51b0b8-b807-4b29-a397-f8090da14749",
   "metadata": {},
   "source": [
    "### ② mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "9ed9631b-3a3b-40fd-b24c-a4784423d467",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.6666666666666666)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(n5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "1a6e35e5-f53d-4478-8d69-ebb749e3d122",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.66666667,  1.        ,  1.66666667])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(n5, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "f0a51378-3e19-4b02-a184-06ad09e40648",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6.66666667, -3.        , -1.66666667])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(n5, axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80b2b8bb-6d93-478a-92b9-fb5e6fc40618",
   "metadata": {},
   "source": [
    "### ③ max"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "69e70a43-e009-4747-96a2-83c137d65c91",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int32(8)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.max(n5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "2182a6e6-09d6-439c-a944-4bdff219f881",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6, 8, 6], dtype=int32)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.max(n5, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "2df6d774-d60a-4f48-9ea6-55a11360ee2a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 4, 5], dtype=int32)"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.max(n5, axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7172a8c6-bb6e-430c-9e3a-abd85c988b05",
   "metadata": {},
   "source": [
    "### ④ min"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "c042f5d1-c598-4b1d-9492-d8ff45d24af3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int32(-10)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.min(n5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "1f5b00be-96a0-4d75-8be8-50eb388c226e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ -5, -10,  -5], dtype=int32)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.min(n5, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "8298044e-1cdc-4693-a353-1d36b75aaf21",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  6, -10,  -5], dtype=int32)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.min(n5, axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22c71ccf-d8a6-4215-80a0-59bae366e3fa",
   "metadata": {},
   "source": [
    "### ⑤ average"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "e947647b-bf95-43ea-b6fe-6f3a8e8cd236",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1 = np.arange(1, 11)\n",
    "n1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "50949c15-b6f7-4d20-aaee-12a3fc90f775",
   "metadata": {},
   "outputs": [],
   "source": [
    "weight = np.random.randint(1, 5, size=(10,))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "2d7d4c19-609c-4250-b6d2-95c2e0fe45f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 4, 2, 4, 3, 1, 4, 3, 3, 3], dtype=int32)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "weight"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "9cf32351-e63c-439d-9ebf-5a56e41dd660",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(5.75)"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.average(n1, weights=weight)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e49c44a-e06a-4fdc-8bdd-1e2cf60299dd",
   "metadata": {},
   "source": [
    "### ⑥ median"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "71ad2daa-5b39-4237-842c-028c954f62c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "b37e468c-22a8-4fc9-b79d-f2bd9157586d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(5.5)"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.median(n1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "27101a8f-81e6-4d2f-8e7d-ad9c41ccf114",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5],\n",
       "       [ 6,  7,  8,  9, 10]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1.reshape(2, -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "3988dc42-57ff-4eae-96c0-8ed4ab790475",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3.5, 4.5, 5.5, 6.5, 7.5])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.median(n1.reshape(2, -1), axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25a4c29c-dc13-4f54-b759-3a9946c37b75",
   "metadata": {},
   "source": [
    "### ⑦ std"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "be97e986-cc52-4b9b-896d-e440aa5de59c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "d6c2e76b-9b75-4ba9-a508-69898187a4d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(5.5)"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(n1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "7d1d0056-a9cb-4515-ab73-0ec06756b82b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-4.5, -3.5, -2.5, -1.5, -0.5,  0.5,  1.5,  2.5,  3.5,  4.5])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1 - np.mean(n1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "5d37cc7f-0528-4550-a70e-88cfeb21b621",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([20.25, 12.25,  6.25,  2.25,  0.25,  0.25,  2.25,  6.25, 12.25,\n",
       "       20.25])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(n1 - np.mean(n1))**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "87231126-d3da-4943-97b2-1d5e0e8857fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(82.5)"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum((n1 - np.mean(n1))**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "312cc36a-c31f-4a0f-a246-59978d710b1a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(2.8722813232690143)"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(np.sum((n1 - np.mean(n1))**2) / 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "d858625c-e0b4-4e32-b4d9-b5f1c415a551",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(2.8722813232690143)"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.std(n1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6d6a086-4c05-4b37-ac18-7868efe71ff2",
   "metadata": {},
   "source": [
    "### ⑧ var"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29a6db1c-44be-4ef6-9ae3-29221a617ce7",
   "metadata": {},
   "source": [
    "$$\n",
    "    var = \\frac{\\sum{(y - \\bar{y})^2}}{n}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "faf839e0-20fa-4ac4-826f-d5ad4ce6119b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(8.25)"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum((n1 - np.mean(n1))**2) / 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "c77bbe7e-3c6f-4360-bc60-05eead572785",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(8.25)"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.var(n1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6dc1645-80e5-40c4-a6c9-bc5f9c6f3d5e",
   "metadata": {},
   "source": [
    "### ⑨ percentile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "62143a38-3f04-47d5-b1ab-d63d809ae936",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3.25, 5.5 , 7.75])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "percent = [25, 50, 75]\n",
    "\n",
    "np.percentile(n1, percent)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "925bc715-e83d-495a-a5c3-570b43716fef",
   "metadata": {},
   "source": [
    "### 三、排序函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33dc3e92-61fa-4e26-98f7-7325410b4870",
   "metadata": {},
   "source": [
    "### ① sort"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "ca0ebcbd-38ba-4970-99f4-29da3372b59f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  6,   8,   6],\n",
       "       [ -3, -10,   4],\n",
       "       [ -5,   5,  -5]], dtype=int32)"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "87ede983-c378-422e-b05b-83b1664f6edd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  6,   6,   8],\n",
       "       [-10,  -3,   4],\n",
       "       [ -5,  -5,   5]], dtype=int32)"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sort(n5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8fcb47ca-66ea-4df4-b717-8de38cd197ac",
   "metadata": {},
   "source": [
    "### ② argsort"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "4b21bebc-26e9-4d1c-8a6c-d1e783fb8eb9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  6,   6,   8],\n",
       "       [-10,  -3,   4],\n",
       "       [ -5,  -5,   5]], dtype=int32)"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sort(n5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "c6518a7b-cc45-459f-8320-e1728f4c0f07",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 2, 1],\n",
       "       [1, 0, 2],\n",
       "       [0, 2, 1]])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argsort(n5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e721b203-d047-4507-9928-9397fa286876",
   "metadata": {},
   "source": [
    "### ③ lexsort"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "5d1e39f8-21a6-42b3-8990-c9b1a0b5bd13",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.random.randint(1, 10, size=(10,))\n",
    "arr2 = np.random.randint(1, 10, size=(10,))\n",
    "arr3 = np.random.randint(1, 10, size=(10,))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "5c68be48-acf0-41cb-a5fd-458eb85de7bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 4, 3, 1, 6, 8, 4, 6, 4, 8], dtype=int32)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([6, 5, 5, 9, 6, 8, 8, 4, 8, 1], dtype=int32)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([1, 8, 5, 6, 4, 4, 5, 1, 2, 6], dtype=int32)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(arr1, arr2, arr3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "baea5404-5544-482b-a035-af37b7985c1f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([7, 0, 8, 4, 5, 2, 6, 9, 3, 1])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sort_index = np.lexsort((arr1, arr2, arr3))  # 返回排序后的索引\n",
    "sort_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "d6d86334-5c25-4e50-b7e9-1eb41069cf45",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 4, 1],\n",
       "       [3, 6, 1],\n",
       "       [4, 8, 2],\n",
       "       [6, 6, 4],\n",
       "       [8, 8, 4],\n",
       "       [3, 5, 5],\n",
       "       [4, 8, 5],\n",
       "       [8, 1, 6],\n",
       "       [1, 9, 6],\n",
       "       [4, 5, 8]], dtype=int32)"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过索引获得排好序的数组\n",
    "np.array([[arr1[i], arr2[i], arr3[i]] for i in sort_index])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bb23858d-d697-4331-8eaa-6208252c92d0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
